﻿using DotNetMVC.OA.IBLL;
using DotNetMVC.OA.Model;
using DotNetMVC.OA.Model.Param;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DotNetMVC.OA.UI.Portal.Controllers
{
    public class ActionInfoController : BaseController
    {
        public IActionInfoService ActionInfoService { get; set; }
        public IRoleInfoService RoleInfoService { get; set; }
        public IR_RoleInfo_ActionInfoService R_RoleInfo_ActionInfoService { get; set; }
        short delFlagNormal = (short)Model.Enum.DelFlagEnum.Normal;
        // GET: ActionInfo
        public ActionResult Index()
        {
            return View();
        }

        #region 查询
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllActionInfos()
        {
            //jquery easyui table:{total:50,rows:[{},{}]}
            //easyui table 在初始化的时候自动发送一下两个参数值
            int pageSize = int.Parse(Request["rows"] ?? "10");
            int pageIndex = int.Parse(Request["page"] ?? "1");

            //过滤用户名、备注   
            string schName = Request["schName"];
            string schRemark = Request["schRemark"];
            //初始化查询条件
            var queryParam = new ActionQueryParam
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                Total = 0,
                SchName = schName,
                SchRemark = schRemark
            };

            var pageData = ActionInfoService.LoagPageData(queryParam);

            int total = queryParam.Total;

            var temp = pageData.Select(a => new { a.ID, a.ActionName, a.DelFlag, a.HttpMethod, a.IsMenu, a.MenuIcon, a.ModfiedOn, a.Remark, a.Sort, a.SubTime, a.Url });
            var data = new { total = total, rows = temp.ToList() };
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 添加
        [HttpPost]
        public ActionResult Add(ActionInfo actionInfo)
        {
            actionInfo.DelFlag = delFlagNormal;
            actionInfo.ModfiedOn = DateTime.Now;
            actionInfo.SubTime = DateTime.Now;

            ActionInfoService.Add(actionInfo);
            return Content("ok");
        }
        #endregion

        #region 删除
        public ActionResult BatchDelete(string ids)
        {
            if (string.IsNullOrEmpty(ids))
            {
                return Content("请选中要删除的数据");
            }
            string[] strIds = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            List<long> idList = new List<long>();
            foreach (var strId in strIds)
            {
                idList.Add(long.Parse(strId));
            }
            ActionInfoService.Delete(idList);
            return Content("ok");
        }

        public ActionResult Delete(long id)
        {
            ActionInfoService.Delete(id);
            return Content("ok");
        }
        #endregion

        #region 修改
        [HttpGet]
        public ActionResult Edit(long id)
        {
            ViewData.Model = ActionInfoService.GetEntities(u => u.ID == id).FirstOrDefault();
            return View();
        }

        [HttpPost]
        public ActionResult Edit(ActionInfo ActionInfo)
        {
            ActionInfoService.Update(ActionInfo);
            return Content("ok");
        }
        #endregion

        #region 上传图片处理
        public ActionResult UploadImage()
        {
            var file = Request.Files["fileMenuIcon"];
            string path = "/UploadFiles/UploadImgs/" + Guid.NewGuid().ToString() + "_" + file.FileName;
            file.SaveAs(Request.MapPath(path));
            return Content(path);
        }
        #endregion

        #region 设置角色
        public ActionResult SetRole(long id)
        {
            //当前要设置角色的用户
            ActionInfo action = ActionInfoService.GetEntities(u => u.ID == id).FirstOrDefault();
            //获取所有角色
            ViewBag.AllRoles = RoleInfoService.GetEntities(u => u.DelFlag == delFlagNormal).ToList();
            //获取用户已有角色
            //ViewBag.ExitsRoles = R_RoleInfo_ActionInfoService.GetEntities(r => r.ActionInfoID == id && r.DelFlag == delFlagNormal).Select(r=>r.RoleInfoID);
            ViewBag.ExitsRoles = (from r in action.R_RoleInfo_ActionInfo
                                  where r.DelFlag == delFlagNormal
                                  select r.RoleInfoID).ToList();

            //给视图传值
            return View(action);
        }

        public ActionResult ProcessSetRole(long actionId)
        {
            List<long> setRoleIdList = new List<long>();
            //获取选中name值
            foreach (var key in Request.Form.AllKeys)
            {
                if (key.StartsWith("ckb_"))
                {
                    long roleId = long.Parse(key.Replace("ckb_", ""));
                    setRoleIdList.Add(roleId);
                }
            }
            ActionInfoService.SetRole(actionId, setRoleIdList);

            return Content("ok");
        }
        #endregion
    }
}